package com.example.rbacmanage.controller;

import com.example.rbacmanage.entity.Post;
import com.example.rbacmanage.service.PostService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.rbacmanage.annotation.LogAnnotation;
import com.example.rbacmanage.common.Result;

/**
 * 岗位前端控制层
 *
 * @author zmd
 */
@RestController
@RequestMapping("/system/post")
public class PostController  {
    @Autowired
    private PostService postService;

    /**
     * 分页查询列表
     */
    @PreAuthorize("hasAnyAuthority('system:post:page')")
    @GetMapping(value = "/page")
    public Result page(Post entity) {
        return Result.success(postService.getPage(entity));
    }

    /**
     * 查询列表
     */
    @GetMapping(value = "/list")
    public Result list(Post entity) {
        return Result.success(postService.getAll(entity));
    }
    /**
     * 根据id获取详情
     */
    @GetMapping(value = "/{id}")
    public Result getById(@PathVariable(value = "id") Long id) {
        return Result.success(postService.getById(id));
    }

    /**
     * 新增
     */
    @PreAuthorize("hasAnyAuthority('system:post:add')")
    @LogAnnotation(module = "岗位", operation = "新增")
    @PostMapping("/add")
    public Result save(@RequestBody Post entity) {
         postService.save(entity);
        return Result.success();
    }

    /**
     * 更新
     */
    @PreAuthorize("hasAnyAuthority('system:post:update')")
    @LogAnnotation(module = "岗位", operation = "更新")
    @PutMapping("/update")
    public Result update(@RequestBody Post entity) {
        postService.update(entity);
        return Result.success();
    }

    /**
     * 删除
     */
    @PreAuthorize("hasAnyAuthority('system:post:remove')")
    @LogAnnotation(module = "岗位", operation = "删除")
    @DeleteMapping(value = "/remove")
    public Result deleteByIds(@RequestBody Long[] ids) {
        postService.remove(ids);
        return Result.success();
    }
}
